package com.apes.bi.model;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;


@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = Canvas.CONST_TABLE_NAME)
public class Canvas extends Model {
    public static final String CONST_TABLE_NAME = "bi_canvas";

    @Id
    @Comment("画布编码")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "画布编码")
    @Column(name = "id", length = 60)
    private String id;

    @Name
    @Comment("画布名称")
    @Column(name = "name", length = 200)
    private String name;


    @Comment("描述")
    @Column(name = "description", length = 200)
    private String description;


    @Comment("默认标记")
    @Column(name = "default_flag", length = 200, nullable = false)
    private boolean defaultFlag = false;


    @Comment("模板标记")
    @Column(name = "template_flag", length = 200, nullable = false)
    private boolean templateFlag = false;

    @Comment("类名")
    @Column(name = "class_name", length = 200)
    private String className;


    @OneToMany(mappedBy = "canvas", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<CanvasContent> canvasContents = new HashSet<>();

    public Set<CanvasContent> getCanvasContents() {
        if( canvasContents == null ) return new HashSet<>();
        for (CanvasContent item : canvasContents) {
            item.setCanvas(this);
        }
        return canvasContents;
    }

    public void setCanvasContents(Set<CanvasContent> canvasContents){
        this.canvasContents.clear();
        if (canvasContents != null) {
            this.canvasContents.addAll(canvasContents);
        }
    }


    @OneToMany(mappedBy = "canvas", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<CanvasPostPermit> canvasPostPermits = new HashSet<>();

    public Set<CanvasPostPermit> getCanvasPostPermits() {
        if (canvasPostPermits == null) return new HashSet<>();
        for (CanvasPostPermit item : canvasPostPermits) {
            item.setCanvas(this);
        }
        return canvasPostPermits;
    }

    public void setCanvasPostPermits(Set<CanvasPostPermit> canvasPostPermits){
        this.canvasPostPermits.clear();
        if (canvasPostPermits != null) {
            this.canvasPostPermits.addAll(canvasPostPermits);
        }
    }



    @OneToMany(mappedBy = "canvas", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<CanvasUserPermit> canvasUserPermits = new HashSet<>();

    public Set<CanvasUserPermit> getCanvasUserPermits() {
        if (canvasUserPermits == null) return new HashSet<>();
        for (CanvasUserPermit item : canvasUserPermits) {
            item.setCanvas(this);
        }
        return canvasUserPermits;
    }

    public void setCanvasUserPermits(Set<CanvasUserPermit> canvasUserPermits){
        this.canvasUserPermits.clear();
        if (canvasUserPermits != null) {
            this.canvasUserPermits.addAll(canvasUserPermits);
        }
    }


}
